/*******************************************************
> Copyright (C) 2024 ==IOT== All rights reserved.
> File Name: array2.0.cpp
> Author: IOT
> Website:www.slq.org
> Created Time: 2024年10月22日 星期二 14时41分49秒
***************************************************/

#include "array.h"


/*
函数功能：填充数组
*/
void Array::fill(int val)
{
	for (int i = 0;i < elements; i++) {
		ptr[i] = val;
	}
}

/*
函数功能：交换两个数组
*/
void Array::swap(Array &arr)
{
	int *tmp = ptr;
	ptr = arr.ptr;
	arr.ptr = tmp;
}

/*
函数功能：重载运算符[]，实现数组元素访问
*/
int& Array::operator[](int index)
{
	if (index >= 0 && index < elements)
		return ptr[index];
	else
		throw "index over range"; // 出错时抛出异常
}

#if 1 
/*
函数功能：友元重载<<,因为左操作数另外一个对象
*/
//ostream& operator<<(ostream& out, Array& obj);
ostream& slq::operator<<(ostream& out, Array& obj)
{
	//cout << "elements:" << obj.size();
	out << "raw: ";
	//out.setwidth(5);
	int i = 0;
	for (;i < obj.size() - 1; i++) {
		out << obj[i] << ",";
	}
	out << obj[i] << endl;
	return out;
}
#endif
